<!--Gordana Pejic 191/01-->
<?php
include_once dirname(__FILE__) . '/db.php';
include_once dirname(__FILE__) . '/../inc_mailer/class.phpmailer.php';
include_once dirname(__FILE__) . '/../inc_mailer/class.smtp.php';

function send_mail($to, $name, $subject, $body) {
    $mail = new PHPMailer();

    $mail->IsSMTP();
    $mail->SMTPAuth = true;                  // enable SMTP authentication
    $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
    $mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
    $mail->Port = 465;                   // set the SMTP port

    $mail->Username = "office.sib011@gmail.com";  // GMAIL username
    $mail->Password = "sibsib011";            // GMAIL password

    $mail->From = "office.sib011@gmail.com";
    $mail->FromName = "StayInBelgrade";
    $mail->Subject = $subject;
    //$mail->AltBody    = "This is the body when user views in plain text format"; //Text Body
    $mail->WordWrap = 50; // set word wrap

    $mail->Body = $body;

    $mail->AddAddress($to, $name);

    $mail->IsHTML(true); // send as HTML

    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message has been sent";
    }
}

function valid_email($address) {
    if (preg_match('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$^', $address)) {
        return true;
    }else
        return false;
}

function filled_out($form_vars) {
    foreach ($form_vars as $key => $value) {
        if ((!isset($key)) || ($value == '')) {
            return false;
        }
    }
    return true;
}

//   if (!isset($_POST['submitbutton']))
//       require 'con_registracija.php'; // PROVERI!
//   else

$con = dbConnect('sib');

$fname = $_POST["fname"];
$lname = $_POST["lname"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$country = $_POST["country"];
$city = $_POST["city"];
$username = $_POST["user"];
$pass = $_POST["pass"];
$repass = $_POST["repass"];
$access = 5;

// podesavanje current time-a, gmt+2h
$timest = mktime(date("H") + 2, date("i"), date("s"), date("m"), date("d"), date("Y"));
$today = date("Y-m-d H:i:s", $timest);

//    $pass=md5($pass);
// kod za verifikaciju registracije, kriptovan MD5 algoritmom
$vercode = '0'; // 1 se setuje kada se verifikuje

if (!filled_out($_POST)) {
    header("Location: ../register.php?err=1");
}



if ($pass != $repass)
    header("Location: ../register.php?err=2");

else
if (!valid_email($email))
    header("Location: ../register.php?err=3");

else {
    $res = mysql_query("SELECT * FROM user WHERE UserName='$username'");
    if (mysql_num_rows($res) >= 1)
        header("Location: ../register.php?err=4");

    else {

        $sql = "INSERT INTO user (FirstName, LastName, Email, Phone, Country, City, UserName, Password, VerificationCode, RegistrationDate, Access )
    VALUES
    ('$fname','$lname','$email', '$phone', '$country', '$city', '$username','$pass', '$vercode', '$today' ,'$access')";

        if (!mysql_query($sql, $con)) {
            die('Error: ' . mysql_error());
        }
        else
            header('Location: ../index.php'); // uspesna registracija
    }
}

$res_id = mysql_query("SELECT * FROM user WHERE UserName='$username'", $con);
$row_id = mysql_fetch_array($res_id);
$iduser = $row_id["IDUser"];

mysql_close($con);

// slanje verifikacionog e-maila


$n_s = $fname . ' ' . $lname;
$message="<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body> Dear $n_s,
      <br /><br />Your personal account for the Stay in Belgrade Site has been created!
      <br />To finish sign up process, proceed to the following address: <a href=http://localhost/sib/pages/validation.php?uid=$iduser > Verification Link </a> .

      <br />
      Your personal login username is follows:
      <br />username: $username
      </body>
</html>";

$subject = "Stay in Belgrade - Verification mail";
send_mail($email, $n_s, $subject, $message);
?>
